package com.haoxuewa.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.haoxuewa.dao.AdminDao;
import com.haoxuewa.dao.EssayDao;
import com.haoxuewa.dao.EssayTempDao;
import com.haoxuewa.entity.Essay;
import com.haoxuewa.entity.EssayTemp;
import com.haoxuewa.service.EssayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service
public class EssayServiceImpl extends ServiceImpl<EssayDao, Essay> implements EssayService {
    private final EssayDao essayDao;

    private final EssayTempDao essayTempDao;

    @Autowired
    public EssayServiceImpl(EssayDao essayDao, EssayTempDao essayTempDao) {
        this.essayDao = essayDao;
        this.essayTempDao = essayTempDao;
    }

    @Override
    public Boolean EssayAuditPass(int essayId) {
        EssayTemp essayTemp = essayTempDao.selectById(essayId);
        Essay essay = new Essay();
        essay.setTitle(essayTemp.getTitle());
        essay.setAuthor(essayTemp.getTitle());
        essay.setTime(essayTemp.getTime());
        essay.setText(essayTemp.getText());
        essay.setCover(essayTemp.getCover());
        essay.setPartition(essayTemp.getPartition());
        essay.setCategory(essayTemp.getCategory());
        essay.setTopic(essayTemp.getTopic());
        return essayDao.insert(essay) + essayTempDao.deleteById(essayId) == 2;
    }


    @Override
    public List<Map<String, Object>> cbBasicCourse() {
        String part = "专插本";
        String category = "基础课";
        return essayDao.selectMaps(new QueryWrapper<Essay>()
                .select("topic", "count(*) as quantity")
                .eq("part", part)
                .eq("category", category)
                .groupBy("topic"));
    }

    @Override
    public List<Map<String, Object>> cbSpecializeCourse() {
        String part = "专插本";
        String category = "专业课";
        return essayDao.selectMaps(new QueryWrapper<Essay>()
                .select("topic", "count(*) as quantity")
                .eq("part", part)
                .eq("category", category)
                .groupBy("topic"));
    }

    @Override
    public String getPicture(int essayId) {
        return essayDao.selectById(essayId).getCover();
    }
}